#pragma once
#include "Channel.h"
#include "TimeStamp.h"
#include "noncopyable.h"
#include "EventLoop.h"

#include <climits>
#include <csignal>
#include <unordered_map>
#include <vector>

using std::unordered_map;
using std::vector;

class EventLoop;

//
class Poller : noncopyable
{
public:
    using ChannelList = vector<Channel*>;
    Poller(EventLoop* loop);
    virtual ~Poller();

    virtual TimeStamp poll(int timeoutMs, ChannelList* activeChannels) = 0;

    //Channel管理 必须在EventLoop线程中调用
    //  更新Channel的关注事件
    virtual void updateChannel(Channel* channel) = 0;
    //  析构时移除Channel
    virtual void removeChannel(Channel* channel) = 0;
    //Channel是否已经加入到Poller的监听列表
    virtual bool hasChannel(Channel *channel) const;

    static Poller* newDefaultPoller(EventLoop* loop);

    void assertInLoopThread() const
    {
        _ownerLoop->assertInLoopThread();
    }
protected:
    using ChannelMap = unordered_map<int, Channel*>; // fd->Channel*
    using ChannelMapConIt = ChannelMap::const_iterator;
    ChannelMap _channels; // 监听列表

private:
    EventLoop* _ownerLoop;

};

